package com.zero.util;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class DateUtils {

    /**
     * 获得一个年份的起止日期
     * @param year
     * @return
     */
    public static Map getYearStartAndEnd(int year){
        //设置起始时间
        Calendar begin = Calendar.getInstance();
        begin.set(Calendar.HOUR_OF_DAY, 0);
        begin.set(Calendar.MINUTE, 0);
        begin.set(Calendar.SECOND, 0);
        begin.set(Calendar.MILLISECOND, 0);
        begin.set(year, 0, 1);
        System.out.println("begin=" + begin.getTime());

        //设置终止时间
        Calendar end = Calendar.getInstance();
        end.set(Calendar.HOUR_OF_DAY, 23);
        end.set(Calendar.MINUTE, 59);
        end.set(Calendar.SECOND, 59);
        end.set(Calendar.MILLISECOND, 0);
        end.set(year, 11, 31);
        System.out.println("end=" + end.getTime());

        HashMap<String, Calendar> result = new HashMap<>();
        result.put("beginDate", begin);
        result.put("endDate", end);
        return result;
    }

    /**
     * 获取一个季度的起止日期
     * @param year
     * @param quarter
     * @return
     */
    public static Map<String, Calendar> getQuarterStartAndEnd(int year, int quarter){
        if (quarter< 0 || quarter >=4)
            throw new RuntimeException("不合法的季度参数: " + quarter);
        Calendar begin = Calendar.getInstance();
        begin.set(Calendar.HOUR_OF_DAY, 0);
        begin.set(Calendar.MINUTE, 0);
        begin.set(Calendar.SECOND, 0);
        begin.set(Calendar.MILLISECOND, 0);
        begin.set(Calendar.YEAR, year);

        Calendar end = Calendar.getInstance();
        end.set(Calendar.HOUR_OF_DAY, 23);
        end.set(Calendar.MINUTE, 59);
        end.set(Calendar.SECOND, 59);
        end.set(Calendar.MILLISECOND, 0);
        end.set(Calendar.YEAR, year);

        if (quarter == 0){
            begin.set(Calendar.MONTH, 0);
            begin.set(Calendar.DAY_OF_MONTH, 1);
            end.set(Calendar.MONTH, 2);
            end.set(Calendar.DAY_OF_MONTH, 31);
        }else if (quarter == 1){
            begin.set(Calendar.MONTH, 3);
            begin.set(Calendar.DAY_OF_MONTH, 1);
            end.set(Calendar.MONTH, 5);
            end.set(Calendar.DAY_OF_MONTH, 30);
        }else if (quarter == 2){
            begin.set(Calendar.MONTH, 6);
            begin.set(Calendar.DAY_OF_MONTH, 1);
            end.set(Calendar.MONTH, 8);
            end.set(Calendar.DAY_OF_MONTH, 30);
        }else if (quarter == 3){
            begin.set(Calendar.MONTH, 9);
            begin.set(Calendar.DAY_OF_MONTH, 1);
            end.set(Calendar.MONTH, 11);
            end.set(Calendar.DAY_OF_MONTH, 31);
        }

        HashMap<String, Calendar> result = new HashMap<>();
        result.put("beginDate", begin);
        result.put("endDate", end);
        return result;
    }

    /**
     * 获取当前季度的上一个季度的起止日期
     * @param year
     * @param quarter
     * @return
     */
    public static Map<String, Calendar> getLastQuarterStartAndEnd(int year, int quarter){
        if (quarter < 0 || quarter > 3)
            throw new RuntimeException("不合法的季度参数：" + quarter);
        if (quarter == 0)
            return getQuarterStartAndEnd(year - 1, 3);
        return getQuarterStartAndEnd(year, quarter - 1);
    }

    /**
     * 获取一个月份的起止时间
     * @return
     */
    public static Map<String, Calendar> getMonthStartAndEnd(int year, int month){
        if (month < 0 || month > 11)
            throw new RuntimeException("不合法的月份：" + month);
        Calendar begin = Calendar.getInstance();
        begin.set(Calendar.HOUR_OF_DAY, 0);
        begin.set(Calendar.MINUTE, 0);
        begin.set(Calendar.SECOND, 0);
        begin.set(Calendar.MILLISECOND, 0);
        begin.set(Calendar.YEAR, year);
        begin.set(Calendar.MONTH, month);
        begin.set(Calendar.DAY_OF_MONTH, 1);

        Calendar end = Calendar.getInstance();
        end.set(Calendar.HOUR_OF_DAY, 23);
        end.set(Calendar.MINUTE, 59);
        end.set(Calendar.SECOND, 59);
        end.set(Calendar.MILLISECOND, 999);
        if (month == 11){
            end.set(Calendar.YEAR, year);
            end.set(Calendar.MONTH, month);
            end.set(Calendar.DAY_OF_MONTH, 31);
        }else{
            end.set(Calendar.YEAR, year);
            end.set(Calendar.MONTH, month + 1);
            end.set(Calendar.DAY_OF_MONTH, 1);
            end.add(Calendar.DAY_OF_MONTH, -1);
        }


        HashMap<String, Calendar> result = new HashMap<>();
        result.put("beginDate", begin);
        result.put("endDate", end);

        return result;
    }

    /**
     * 获取今日时间，具体时间均设为0
     * @return
     */
    public static Date getToday(){
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);

        return calendar.getTime();
    }
}
